/**
 * Date: 8/24/20
 */
const {app, BrowserWindow, screen, ipcMain, ipcRenderer, Menu, powerSaveBlocker, crashReporter, Tray, nativeImage, globalShortcut, dialog, nativeTheme} = require('electron');
const path = require('path');
const fs = require('fs');
const url = require('url');


console.log('app-start');
const isDev = process.env.NODE_ENV === 'development';
let mainWindow = null;
const isMac = process.platform === 'darwin';
const isWin = process.platform === 'win32';
let forceQuit = false;


const createWindow = () => {
  console.log('createWindow');
  let size = screen.getPrimaryDisplay().workAreaSize;
  let width = parseInt(size.width * 0.9);
  let height = parseInt(size.height * 0.9);

  mainWindow = new BrowserWindow({
    backgroundColor: 'white',
    titleBarStyle: 'hidden',
    frame: 'darwin' === process.platform,
    acceptFirstMouse: true,
    width: width,
    height: height,
    webPreferences: {
      webSecurity: false,
      nodeIntegration: true,
      preload: path.join(__dirname, '/script/preload.js'),
    },
  })

  // dev
  if (isDev) {
    mainWindow.loadURL('http://localhost:3000/#/');
    mainWindow.webContents.openDevTools();
  } else {
    console.log(`file://${path.resolve(__dirname, '../')}/dist/index.html`);
    mainWindow.loadURL(`file://${path.resolve(__dirname, '../')}/dist/index.html`)
  }

  mainWindow.on('closed', () => {
    console.log('mainWindow:closed');
    mainWindow = null;
    if (process.platform === 'darwin') {
      app.exit();
    }
  });

  mainWindow.on('focus', () => {
    console.log('mainWindow:focus');
  });

  mainWindow.on('close', (e) => {
    if (forceQuit) {
      if (isWin) {
        app.quit();
      } else {
        app.exit();
      }
    } else {
      e.preventDefault();
      mainWindow && mainWindow.hide();
    }
    console.log('mainWindow:close');
  });
  console.log('createWindow-end');
};


app.on('ready', () => {
  console.log('app-ready');
  createWindow();
  const displays = screen.getAllDisplays();
  let externalDisplay = null;
  for (let i in displays) {
    if (displays[i].bounds.x != 0 || displays[i].bounds.y != 0) {
      externalDisplay = displays[i]
      break;
    }
  }
  if (externalDisplay) {
    mainWindow = new BrowserWindow({
      x: externalDisplay.bounds.x + 50,
      y: externalDisplay.bounds.y + 50
    })
  }
  globalShortcut.register('CommandOrControl+Alt+Shift+I', () => {
    mainWindow.toggleDevTools();
  });
  console.log('app-ready-end');
});

// window all closed
app.on('window-all-closed', () => {
  console.log('window-all-closed');
  if (isWin) {
    app.quit();
  } else {
    app.exit();
  }
});

// active
app.on('activate', () => {
  console.log('activate');
  if (!mainWindow.isVisible()) {
    mainWindow.show();
  }
});


// before-quit
app.on('before-quit', (e) => {
  console.log('before-quit');
  forceQuit = true;
  mainWindow = null;
});

app.on('will-quit', () => {
  console.log('will-quit');
});

process.on('uncaughtException', error => {
  console.error('uncaughtException', error.stack || JSON.stringify(error));
});
